import {createStore} from 'redux'
interface IState{
  list:object
}
let Initstate = {
    list:[],
    user:[
        {
            "userName":"aaa",
            "passWord":111
        },  {
            "userName":"bbb",
            "passWord":222
        },  {
            "userName":"ccc",
            "passWord":333
        }
    ],
    loginIn:false,
}
function reducer(state:any=Initstate,action:any){
    state=JSON.parse(JSON.stringify(state))
    switch (action.type){
        case 'GET_DATA':
            state.list=action.data
            return state
        case 'LOGIN_NOW':
            let str = state.user.filter((item:any)=>item.userName==action.user)[0]
            if(!str&&action.pass!=null){
                    alert("该用户未注册")
            }else if(str&&action.pass==''){
                alert("请输入密码！")
            }else if(str&&str.passWord!=action.pass){
                alert("密码错误，请重新输入！")
            }else if(str&&str.passWord==action.pass){
                state.loginIn=true
            }
            return state
        case 'ADD_REGISTORY':
            state.user.push({"userName":action.user,"passWord":action.pass*1})
            return state
        case 'ADD_VOID':
           state.list.filter((item:any)=>{
                return  item.id==action.bID
            })[0].select[action.sID].size++     
            return state
        default:
            return state
    }
}
const store = createStore(reducer)
export default store